package com.wq.biz;

import java.awt.Point;


/**
 * 棋盘控制类
 * @author wq
 * 2020/12/19 - 17:43
 *
 */
public class ChessPane {
	public static final int INI_LEN = -1; //初始绝对位置
	public static final int OVER_LEN = 56; //终点绝对位置
	
	public static Point testP = null; 
	
	private static final Point[] LU = {//0
			new Point(75,50),
			new Point(150,50),
			new Point(75,125),
			new Point(150,125),
			new Point(50,205)};
	private static final Point[] LD = {//1
			new Point(75,590),
			new Point(150,590),
			new Point(75,665),
			new Point(150,665),
			new Point(213,675)};
	private static final Point[] RU = {//2
			new Point(625,55),
			new Point(700,55),
			new Point(625,125),
			new Point(700,125),
			new Point(555,43)};
	private static final Point[] RD = {//3
			new Point(625,590),
			new Point(700,590),
			new Point(625,665),
			new Point(700,665),
			new Point(720,515)
	};
	
	private static final Point[] ABSOLUTE_POSITION = {
			new Point(253,654),			new Point(234,607),			new Point(235,565),
			new Point(253,519),			new Point(215,490),			new Point(170,500),
			new Point(130,500),			new Point(85,488),			new Point(65,440),
			new Point(65,400),			new Point(65,357),			new Point(65,315),
			new Point(65,276),			new Point(79,235),			new Point(127,213),
			new Point(170,212),			new Point(215,230),			new Point(247,205),
			new Point(232,153),			new Point(233,114),			new Point(251,66),
			new Point(296,47),			new Point(340,47),			new Point(389,46),
			new Point(427,46),			new Point(466,46),			new Point(510,65),
			new Point(521,113),			new Point(530,150),			new Point(515,202),
			new Point(543,230),			new Point(595,215),			new Point(639,215),
			new Point(685,233),			new Point(696,273),			new Point(699,320),
			new Point(700,360),			new Point(698,400),			new Point(700,440),
			new Point(683,485),			new Point(635,500),			new Point(600,500),
			new Point(555,492),			new Point(521,528),			new Point(536,571),
			new Point(521,602),			new Point(507,647),			new Point(469,666),
			new Point(426,666),			new Point(382,666),			new Point(336,666),
			new Point(299,666)};
	
	private static final Point[][] OVER_ROAD = {
			new Point[]{new Point(384,607),
				new Point(384,565),				new Point(384,519),
				new Point(384,484),				new Point(384,440),
				new Point(384,396)},
			new Point[]{new Point(130,355),
				new Point(174,355),				new Point(211,355),
				new Point(256,355),				new Point(293,355),
				new Point(337,355)},
			new Point[]{new Point(382,110),
				new Point(383,152),				new Point(384,193),
				new Point(384,232),				new Point(382,273),
				new Point(384,312)},
			new Point[]{new Point(628,355),
				new Point(592,355),				new Point(552,355),
				new Point(510,355),				new Point(464,355),
				new Point(428,355)}
	};
	
	private static final int[] RELATIVE_SPECIAL = {2,6,10,14,22,26,30,34,38,42,46};
	private static final int RELATIVE_SKIP = 18;
	public static final int SPECIAL_LEN = 4,SKIP_LEN = 12;
	private static ChessPane cp = new ChessPane();
	private ChessPane()
	{
		
	}
	public static ChessPane getChessPane()
	{
		return cp;
	}
	private int playerNum;
	private int[] iniPosition;
	private Point[] points = {};
	private int len = 68;
	private Point[][] iniPoints ;
	
	
	
	public void iniChess(int playerNum)
	{
		this.playerNum = playerNum;
		iniPoints = new Point[4][5];
		
		iniPoints = new Point[][]{LD,LU,RU,RD};
		
	}
	public void iniPlanes(Plane[] plane)
	{
		for(int c = 0 ; c < plane.length ;c++)
		{
			plane[c].setLocation(iniPoints
					[plane[c].getMaster()]
					[plane[c].getIndex()]
							);
		}
	}
	public Point[] getNextPoint(int master,int planeIndex ,int len,int addLen)
	{//CHANGE
//		if(testP != null)
//		{
////			Point p = testP;
////			testP = null;
//			return new Point[]{ABSOLUTE_POSITION[len+1]};
//		}
		
		
		if(len == -1)
		{
			return new Point[]{iniPoints[master][4]};
		}else
		{
			if(len+addLen > 50)
			{
				int overLen = len+addLen;
				if(overLen == OVER_LEN)
				{
					return new Point[]{OVER_ROAD[master][5],iniPoints[master][planeIndex]};
				}else
				{
					if(overLen > 56)
					{
						int more = 5 + (56-overLen);
						return new Point[]{OVER_ROAD[master][5],OVER_ROAD[master][more]};
					}else
					{
						int more = len-50+addLen-1;
						return new Point[]{OVER_ROAD[master][more]};
					}
				}
			}
			
			
			int start = master*13 + len;
			int index = master*13 + len + addLen;
			System.out.println("0 , start" + master*13 + "-" + len + " end "+ index);
			while(start > 52)
			{
				start -= 52;
			}
			while(index > 52)
			{
				index -= 52;
			}
			System.out.println("1 , start" + start+ " end "+ index);
			return getLenPoint(master*13 + len, index);
//			return new Point[]{ABSOLUTE_POSITION[index]};
		}
		
		
//		return new Point[]{new Point((int) (50+Math.random()*50),(int) (200+Math.random()*200))};
	}
	
	private Point[] getLenPoint(int start , int end)
	{//CHANGE
		

		
		int len = end-start;
		if(end < start)
		{
			len = ABSOLUTE_POSITION.length - start + end;
		}
		if(len > 7)
		{
			return new Point[]{ABSOLUTE_POSITION[end-1]};
		}
		Point[] p = new Point[len];
		for(int c = 0 ; c< len;c++)
		{
			if(start + c< ABSOLUTE_POSITION.length-1)
			{
				p[c] = ABSOLUTE_POSITION[start+c];
			}else
			{
				p[c] = ABSOLUTE_POSITION[end - (len -c)];
				//56  52  2
			}
		}
		return p;
	}
	
	public int getAbsoluteLen(int master,int len)
	{//CHANGE
		if(len == -1 || len == 0 || len > 50)
		{
			return -2;
		}
//		System.out.println("master Len " +master +""+  (master*13 + len));
		int abLen = master*13 + len;
		while(abLen > 52)
		{
			abLen -= 52;
		}
		
		
		return abLen;
	}
	
	public Point[] getHomePoint(int master,int index)
	{
		return new Point[]{iniPoints[master][index]};
	}
	public boolean isSpecial(int len)
	{
		for(int c = 0 ; c < RELATIVE_SPECIAL.length ;c++)
		{
			if(len == RELATIVE_SPECIAL[c])
			{
				return true;
			}
		}
		
		return false;
	}
	public boolean isSkip(int len)
	{
		return len == RELATIVE_SKIP;
	}
	
	
}
